<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import VersionCard from './version-card.vue'
import router from '../router'
import { storeToRefs } from 'pinia'
import { useToolStore } from '../stores/tool'

declare global {
  interface Window {
    pywebview: {
      api: any
    },
    debug: (text: string, type: any) => void
    setTaskBarContent: (text: string, progress: number) => void
  }
}

const {
  selectedConda, rootUrl
} = storeToRefs(useToolStore())

const link_urls = ref('') // anaconda实际的下载链接
const currentVersion = ref({ // 传递给version-card组件的参数，显示本地安装的python版本信息
  platform: 'null',
  conda: 'null',
  python: 'null',
  url: 'Anaconda3-2024.10-1-Windows-x86_64.exe',
  exist: true
})

const versions = computed(() => { // 传递给version-card组件的参数，用于显示所有可用的python版本
  if (link_urls.value === '') {
    return []
  }
  const _links = JSON.parse(link_urls.value)
  rootUrl.value = _links.anaconda.root_url
  return _links.anaconda.versions as { platform: string, conda: string, python: string, url: string, exist?: boolean }[]
})

onMounted(() => {
  if (window.pywebview) { // 在pywebview框架里，从python获取信息
    window.pywebview.api.get_key('anaconda link').then((res: string) => {
      link_urls.value = res
    })
    window.pywebview.api.get_key('current version').then((res: string) => {
      console.log('version', JSON.parse(res))
      currentVersion.value = { ...JSON.parse(res), exist: true }
    })
  } else { // 当pywebview不存在时，给参数默认值，方便前端调试
    link_urls.value = '{"anaconda": {"root_url": "https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/", "versions": [{"platform": "windows x86_64", "conda": "anaconda3 2024.10", "python": "3.12.9", "url": "Anaconda3-2024.10-1-Windows-x86_64.exe"}, {"platform": "linux x86_64", "conda": "anaconda3 2024.10", "python": "3.12.9", "url": "Anaconda3-2024.10-1-Linux-x86_64.sh"}, {"platform": "linux aarch64", "conda": "anaconda3 2024.10", "python": "3.12.9", "url": "Anaconda3-2024.10-1-Linux-aarch64.sh"}, {"platform": "linux s390x", "conda": "anaconda3 2024.10", "python": "3.12.9", "url": "Anaconda3-2024.10-1-Linux-s390x.sh"}, {"platform": "windows x86_64", "conda": "anaconda3 2023.07", "python": "3.11.4", "url": "Anaconda3-2023.07-2-Windows-x86_64.exe"}, {"platform": "linux x86_64", "conda": "anaconda3 2023.07", "python": "3.11.4", "url": "Anaconda3-2023.07-2-Linux-x86_64.sh"}, {"platform": "linux aarch64", "conda": "anaconda3 2023.07", "python": "3.11.4", "url": "Anaconda3-2023.07-2-Linux-aarch64.sh"}, {"platform": "linux ppc64le", "conda": "anaconda3 2023.07", "python": "3.11.4", "url": "Anaconda3-2023.07-2-Linux-ppc64le.sh"}, {"platform": "linux s390x", "conda": "anaconda3 2023.07", "python": "3.11.4", "url": "Anaconda3-2023.07-2-Linux-s390x.sh"}, {"platform": "windows x86_64", "conda": "anaconda3 2023.03", "python": "3.10.9", "url": "Anaconda3-2023.03-Windows-x86_64.exe"}, {"platform": "linux x86_64", "conda": "anaconda3 2023.03", "python": "3.10.9", "url": "Anaconda3-2023.03-Linux-x86_64.sh"}, {"platform": "linux aarch64", "conda": "anaconda3 2023.03", "python": "3.10.9", "url": "Anaconda3-2023.03-Linux-aarch64.sh"}, {"platform": "linux ppc64le", "conda": "anaconda3 2023.03", "python": "3.10.9", "url": "Anaconda3-2023.03-Linux-ppc64le.sh"}, {"platform": "linux s390x", "conda": "anaconda3 2023.03", "python": "3.10.9", "url": "Anaconda3-2023.03-Linux-s390x.sh"}, {"platform": "windows x86_64", "conda": "anaconda3 2022.10", "python": "3.9.13", "url": "Anaconda3-2022.10-Windows-x86_64.exe"}, {"platform": "linux x86_64", "conda": "anaconda3 2022.10", "python": "3.9.13", "url": "Anaconda3-2022.10-Linux-x86_64.sh"}, {"platform": "linux aarch64", "conda": "anaconda3 2022.10", "python": "3.9.13", "url": "Anaconda3-2022.10-Linux-aarch64.sh"}, {"platform": "linux ppc64le", "conda": "anaconda3 2022.10", "python": "3.9.13", "url": "Anaconda3-2022.10-Linux-ppc64le.sh"}, {"platform": "linux s390x", "conda": "anaconda3 2022.10", "python": "3.9.13", "url": "Anaconda3-2022.10-Linux-s390x.sh"}, {"platform": "windows x86_64", "conda": "anaconda3 2021.05", "python": "3.8.8", "url": "Anaconda3-2021.05-Windows-x86_64.exe"}, {"platform": "windows x86", "conda": "anaconda3 2021.05", "python": "3.8.8", "url": "Anaconda3-2021.05-Windows-x86.exe"}, {"platform": "linux x86_64", "conda": "anaconda3 2021.05", "python": "3.8.8", "url": "Anaconda3-2021.05-Linux-x86_64.sh"}, {"platform": "linux aarch64", "conda": "anaconda3 2021.05", "python": "3.8.8", "url": "Anaconda3-2021.05-Linux-aarch64.sh"}, {"platform": "linux ppc64le", "conda": "anaconda3 2021.05", "python": "3.8.8", "url": "Anaconda3-2021.05-Linux-ppc64le.sh"}, {"platform": "linux s390x", "conda": "anaconda3 2021.05", "python": "3.8.8", "url": "Anaconda3-2021.05-Linux-s390x.sh"}, {"platform": "windows x86_64", "conda": "Anaconda3-uninstaller-patch-win-64-2023.07-0.exe", "python": "None", "url": "Anaconda3-uninstaller-patch-win-64-2023.07-0.exe"}]}, "pip": {"mirror": ""}}'
    currentVersion.value = {
      platform: 'windows x86_64',
      conda: 'anaconda3 2023.03',
      python: '3.9.13',
      url: 'Anaconda3-2024.10-1-Windows-x86_64.exe',
      exist: true
    }
  }

})

const existCurrentVersion = computed(() => {
  return currentVersion.value.python !== 'null'
})

const next = () => {
  if (!selectedConda.value) {
    alert('请先选择待安装的python版本')
    return
  }
  if (selectedConda.value.platform !== currentVersion.value.platform) {
    alert(`选择的版本不适用于本机的架构${currentVersion.value.platform}`)
    return
  }
  router.push('/online/online2')
}

const selectedFile = computed(() => {
  if (selectedConda.value && selectedConda.value.url) {
    return selectedConda.value.url
  } else {
    return ''
  }
})
</script>

<template>
  <div class="w-full h-full">
    <div class="w-full h-full flex flex-col justify-center">
      <div class="w-full flex-none text-center py-4">
        <label class="w-full text-xl">选择安装的python和anaconda版本</label>
        <button @click="next()"
                class="border-2 bg-green-100 border-black rounded border-lime-500 mx-4 px-4 hover:bg-green-300">下一步
        </button>
        <div>{{ selectedFile }}</div>
      </div>
      <!--      <div class="flex flex-row justify-evenly py-2 select-none">-->
      <!--        <span><input id="m1" name="method" type="radio" /><label for="m1" disabled>基于本机python版本</label></span>-->
      <!--        <span><input id="m2" name="method" type="radio" /><label for="m2">下载在线python版本</label></span>-->
      <!--      </div>-->
      <div class="flex-1 bg-red-300 flex flex-row flex-wrap gap-2 justify-evenly">
        <div v-if="existCurrentVersion">
          <version-card :version="currentVersion"></version-card>
        </div>
        <div v-for="version in versions">
          <version-card :version="version"></version-card>
        </div>
      </div>
      <div class="h-10 flex-none"></div>
      <div class="flex-1 bg-white"></div>
    </div>
  </div>
</template>

<style scoped>

</style>